package com.xiaoyu.lanling.feature.moment.data.detail

import `in`.srain.cube.request.JsonData
import `in`.srain.cube.request.RequestDefaultHandler
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import com.xiaoyu.base.net.RequestCreator
import com.xiaoyu.lanling.common.config.HttpConfig
import com.xiaoyu.lanling.event.moment.MyMomentListEvent
import com.xiaoyu.lanling.feature.moment.model.MomentItem

class MyMomentListData(val uid: String) : PagedListDataModelWithRequestTag<MomentItem, Any>() {

    val tag = Any()

    override fun doQueryData() {
        RequestCreator.create<MyMomentListEvent>(MyMomentListEvent::class.java)
                .addQueryData("uid", uid)
                .appendQueryDataTo(listPageInfo)
                .setRequestUrl(HttpConfig.LANLING_MOMENT_LIST)
                .setPostEventWhenFail(true)
                .setRequestHandler(object : RequestDefaultHandler<MyMomentListEvent, JsonData>() {
                    override fun processOriginData(originData: JsonData): MyMomentListEvent {
                        val rawData = originData.optJson("data")
                        val rawList = rawData.optJson("list").toList()
                        val list = mutableListOf<MomentItem>()
                        for (json in rawList) {
                            list.add(MomentItem.createProfileMoment(json))
                        }
                        updateResult(rawData, list)
                        return MyMomentListEvent(requestTag)
                    }

                    override fun onRequestFinish(data: MyMomentListEvent) {
                        data.post()
                    }
                })
                .enqueue()
    }

    override fun getRequestTag(): Any {
        return tag
    }
}